<template>
    <section>
        <div style="margin-bottom:5px;">
            <el-input placeholder="请输入内容" size="small" v-model="searchKey" style="width: 250px;">
                <el-button slot="append" size="small" icon="el-icon-search" @click="getDataList"></el-button>
            </el-input>
            <el-button type="primary" size="small" @click="openAddDialog()">新 建</el-button>
        </div>
        <el-table ref="multipleTable" :data="dataList" border tooltip-effect="dark" style="width: 100%"
                  @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" label="序号">
            </el-table-column>
            <el-table-column prop="appCode" label="应用编码">
            </el-table-column>
            <el-table-column prop="appName" label="应用名称">
            </el-table-column>
            <el-table-column prop="accessPre" label="访问前缀">
            </el-table-column>
            <el-table-column prop="appType" label="应用类型">
            </el-table-column>

            <el-table-column fixed="right" label="操作" width="300">
                <template slot-scope="scope">
                    <el-button-group>

                        <el-button size="mini" type="primary" @click="openDialog(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="removeItem(scope.row)">删除</el-button>
                    </el-button-group>
                </template>
            </el-table-column>
        </el-table>
        <div style="text-align: right;background: rgb(239, 242, 247);border: 1px solid #E5E9F2;">
            <el-pagination style="line-height: 30px; height: 30px;" @size-change="handleSizeChange"
                           @current-change="handleCurrentChange" :current-page="currentPage"
                           :page-sizes="[10, 20, 30, 50]" :page-size="pageSize"
                           layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
        <div v-if="dialogFormVisibleAdd">
            <el-dialog :title="dialogAddOrEdit" :visible="dialogFormVisibleAdd" :modal="dialogFormVisibleAdd"
                       :before-close="openAddDialogClose">

                <el-form label-width="100px" :rules="formAddRules" :model="formAdd" ref="formAdd">

                    <el-row>
                        <el-col :span=12>
                            <el-form-item label="应用编码" prop="appCode">
                                <el-input v-model="formAdd.appCode" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :span=12>
                            <el-form-item label="应用名称" prop="appName">
                                <el-input v-model="formAdd.appName" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-col>

                    </el-row>

                    <el-row>
                        <el-col :span=12>
                            <el-form-item label="访问前缀" prop="accessPre">
                                <el-input v-model="formAdd.accessPre" style="width: 100%"></el-input>
                                <div>
                                    {{visitAccess}}
                                </div>
                            </el-form-item>
                        </el-col>

                        <el-col :span=12>
                            <el-form-item label="应用类型" prop="appType">
                                <el-input v-model="formAdd.appType" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-col>

                    </el-row>
                    <el-row>
                        <el-col :span=12>
                            <el-form-item label="swagger地址" prop="swaggerUrl">
                                <el-input 　 v-model="formAdd.swaggerUrl"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span=12>
                            　
                            <el-form-item label="排序号" prop="sequence">
                                <el-input 　 v-model="formAdd.sequence"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    　
                    <el-row>
                        <el-col :span=24>
                            <el-table
                                    :data="formAdd.machineList"
                                    border
                                    style="width: 100%">
                                <el-table-column
                                        label="机器ID"
                                        　>
                                    <template slot-scope="scope">
                                        <el-input v-model="scope.row.serverId" placeholder="请输入机器ID"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                        label="目标地址"
                                        　>
                                    <template slot-scope="scope">
                                        <el-input v-model="scope.row.machineHost" placeholder="请输入目标地址"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                        label="权重"
                                        　>
                                    <template slot-scope="scope">
                                        <el-input v-model="scope.row.weight" placeholder="请输入权重"
                                        ></el-input>
                                    </template>
                                </el-table-column>

                                <el-table-column label="操作" 　:render-header="renderHeader" 　>
                                    <template slot-scope="scope">
                                        <el-button-group>
                                            <el-button size="mini" type="danger" @click="renderRemoveRow(scope.$index)">
                                                删除
                                            </el-button>
                                        </el-button-group>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </el-col>
                    </el-row>
                    <el-form-item>
                        <el-button type="primary" @click="submitFormAdd('formAdd')">保存</el-button>
                        <el-button @click="openAddDialogClose">取消</el-button>
                    </el-form-item>
                </el-form>
            </el-dialog>
        </div>
    </section>
</template>
<style>

</style>
<script>
    import {AjaxByAll} from '@/api/api';

    export default {
        data() {

            return {
                dataList: [],
                multipleSelection: [],
                dialogFormVisibleAdd: false,
                labelPosition: 'left',
                formAdd: {
                    id: null,
                    appCode: null,
                    appName: null,
                    accessPre: null,
                    appType: null,
                    sequence: null,
                    swaggerUrl: null,
                    machineList: [],
                },
                projectCode: null,
                pageSize: 10,
                currentPage: 1,
                total: 0,
                dialogAddOrEdit: '',
                searchKey: null, defaultData: null,
                formAddRules: {
                    appCode: [
                        {required: true, message: '请填写应用编码', trigger: 'blur'}
                    ],
                    appName: [
                        {required: true, message: '请填写应用名称', trigger: 'blur'}
                    ],
                    accessPre: [
                        {required: true, message: '请填写访问前缀', trigger: 'blur'}
                    ],
                }
            }
        },

        methods: {

            renderHeader(h, params) {
                let a = [h('el-button', {
                    props: {
                        size: "mini",
                        type: "primary"
                    },
                    on: {
                        click: () => {
                            this.renderAddRow();
                        }
                    }
                }, '增加')];
                return h('div', a);
            },
            renderAddRow() {
                this.formAdd.machineList.push({"serverId": "", "machineHost": "", "weight": ""});
            },
            renderRemoveRow(index) {

                this.formAdd.machineList.splice(index, 1);
            },
            openDialog(model) {

                if (model) {
                    let id = model.id;
                    this.dialogAddOrEdit = '编辑网关应用';
                    let urls = 'gateway/app/' + id;
                    let method = 'get';
                    let form = {};
                    AjaxByAll(method, urls, form).then(data => {
                        if (data.code === 200) {
                            this.formAdd = data.data;
                        } else {
                            this.$message.error(data.msg);
                        }
                    });
                }
                this.dialogFormVisibleAdd = true;
            },
            openAddDialog() {
                this.dialogAddOrEdit = '新建网关应用';
                // 送出後
                this.$data.formAdd = Object.assign(this.$data.formAdd, this.defaultData);

                this.dialogFormVisibleAdd = true;
            },
            openAddDialogClose() {

                this.dialogFormVisibleAdd = false;
            },
            getDataList() {
                let self = this;
                let form = {
                    currentPage: self.currentPage,
                    pageSize: self.pageSize,
                    keyword: self.searchKey,
                };
                let urls = 'gateway/app/query';
                AjaxByAll('post', urls, form).then(data => {
                    if (data.code === 200) {

                        self.total = data.data.total * 1;
                        self.dataList = data.data.records;
                    }

                    this.loading = false;
                });
            },
            submitFormAdd(formName) {

                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let form = this.formAdd;
                        let urls = 'gateway/app/';
                        let method = this.formAdd.id == null ? 'post' : 'put';
                        AjaxByAll(method, urls, form).then(data => {
                            if (data.code === 200) {
                                this.formAdd.id = data.data.id;
                                this.$message({
                                    message: '操作成功',
                                    type: 'success'
                                });

                                this.getDataList();
                            } else {
                                this.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                            this.dialogFormVisibleAdd = !this.dialogFormVisibleAdd;
                        });


                    } else {
                        this.$message({
                            message: '校验失败',
                            type: 'error'
                        });
                    }
                });
            },
            removeItem(model) {
                this.$confirm('确认移除吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    AjaxByAll('delete', 'gateway/app/' + model.id, null).then(data => {
                        if (data.code === 200) {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                            this.getDataList();
                        } else {
                            this.$message({
                                message: '操作失败：' + data.msg,
                                type: 'error'
                            });
                        }

                    });
                }).catch(() => {

                });
            },
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleSizeChange(val) {
                //  console.log(`每页 ${val} 条`);
                this.pageSize = val;
                this.getDataList();
            },
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                this.currentPage = val;
                this.getDataList();
            }
        },
        created() {
            this.defaultData = JSON.parse(JSON.stringify(this.formAdd));
            this.projectCode = this.$route.params.projectCode;


        },// 计算属性
        computed: {
            visitAccess () {
                return "访问接口地址为:http://网关地址/"+this.projectCode+"/"+this.formAdd.accessPre;
            }
        },
        mounted: function () {
            this.getDataList();
            this.defaultData = JSON.parse(JSON.stringify(this.formAdd));
        }
    }
</script>
